﻿using DFGK.WMS.Common.Tools;
using DFGK.WMS.Common.Web;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NetTaste;
using NPOI.POIFS.Crypt.Dsig;

namespace DFGK_WMS.WebAPI.Controllers.设备
{
    /// <summary>
    /// 立库
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WCSController : ControllerBase
    {
        public ITPutawayOrderRepository TPutawayOrderService { get; set; }

        public ITCellRepository TCellRepository { get; set; }

        private readonly ILogRepository logRepository;
        public WCSController(ITPutawayOrderRepository tPutawayOrderRepository,ITCellRepository tCellRepository, ILogRepository logRepository)
        {
            TPutawayOrderService = tPutawayOrderRepository;
            TCellRepository = tCellRepository;
            this.logRepository = logRepository;
        }
        /// <summary>
        /// 出入库任务状态上报
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult TasklikuAppear(LiKuDto dto)
        {
            if (dto.method != "end")
            {
                return Ok(BaseResult.ReturnInfo(1, "", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            }
            var order = TPutawayOrderService.Getputawayorder(x => x.Code == dto.taskCode);
            //查询任务
            if (order == null)
            {
                return Ok(BaseResult.ReturnInfo(1, "未找到该任务", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            }
            string message = "";
            //入库任务
            if (order.SourceType == 1)
            {
                //调用库存接口
                //任务修改
                if (order.BillType == 6)
                {
                    order.Status = 5;
                    if (!TPutawayOrderService.UpdatePutawayorder(order))
                    {
                        return Ok(BaseResult.ReturnInfo(1, "任务修改失败", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                    }
                }
                else
                {
                bool result = TPutawayOrderService.ExecInstructFinished(order, out message);
                if (!result)
                    return Ok(BaseResult.Result(false, "出库失败" + message, "500"));
                }
                var ip = LedipShow.Getledip(order.SourceType, order.Roadway);
                HttpHelper.LedSendPostRequest(new { ip = ip, message = "中煤科工西安研究院\\n(集团)有限公司\\n欢迎领导莅临检查" });

            }
            //出库任务
            else if (order.SourceType == 2)
            {
                var waydata = new { taskCode = order.Code, Roadway = order.Roadway };
                var waydate = HttpHelper.SendPostRequestes("获取巷道节点", waydata);
                LogHelper.Info("获取巷道节点:" + waydate);
                //输送线出库
                //var data = new { taskCode = order.Code, startPoint = waydate.data, endPoint = order.SiteCode };
                TaskOutDto data = new TaskOutDto()
                {
                    taskCode = order.Code,
                    barCode = order.PalletBarcode,
                    startPoint = waydate.data,
                    endPoint = order.SiteCode
                };
                var lindata = HttpHelper.SendPostRequest("出库任务", data);
                LogHelper.Info("出库任务：" + lindata);
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 4;
                logg.ModuleName = "设备接口";
                logg.UserName = "输送线";
                logg.RealName = "输送线";
                logg.UserId = 0;
                logg.Description = lindata;
                //获取前端IP地址

                logRepository.AddTLod(logg);
                return Ok(BaseResult.ReturnInfo(0, lindata, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            }
            else if(order.SourceType == 3)
            {
                bool result = TPutawayOrderService.CompleteTransferBinOrders(order, out message);
                if (!result)
                    return Ok(BaseResult.Result(false, "移库失败" + message, "500"));
            }
            return Ok(BaseResult.ReturnInfo(0, "", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
        }

        /// <summary>
        /// 出入库任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult TaskReceive(TaskLiku task)
        {
            var data= HttpHelper.WCSSendPostRequest("Api/ApiWCS/ReceiveTask", task);
            LogHelper.Info("wcs出入库：" + data);
            return Ok(BaseResult.ReturnInfo(0, data, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
        }
    }
}
